package cn.iocoder.yudao.module.member.service.siteconfiguration;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.SiteConfigurationVo;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.UserPoolConfigDetailVo;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.UserPoolConfigVo;
import cn.iocoder.yudao.module.member.convert.siteconfiguration.UserPoolConfigDetailConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.siteconfiguration.UserPoolConfigDetailDo;
import cn.iocoder.yudao.module.member.dal.mysql.siteconfiguration.UserPoolConfigDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

@Service
@Validated
public class UserPoolConfigDetailServiceImpl implements UserPoolConfigDetailService{


    @Resource
    private UserPoolConfigDetailMapper userPoolConfigDetailMapper;

    /**
     * 保存前端站点配置信息
     * @param list
     */
    @Override
    public void saveConfig(List<UserPoolConfigVo> list) {
        list.forEach(t->{
//            t.getDetails().forEach(  {a->a.setSiteUserCode(t.getSiteUserCode()) });
            t.setId(null);
            if (CollUtil.isEmpty(t.getDetails())){
                throw exception(INCOMPLETE_CONFIGURATION, t.getVersionTypeName());
            }
            if (!CollUtil.isEmpty(t.getDetails())){
                t.getDetails().forEach(a->{
                    a.setSiteUserCode(t.getSiteUserCode());
                    a.setSiteCode(t.getSiteCode());
                    a.setVersionTypeCode(t.getVersionTypeCode());
                    a.setVersionTypeName(t.getVersionTypeName());
                    a.setId(null);
                });
            }

            userPoolConfigDetailMapper.insertBatch(UserPoolConfigDetailConvert.INSTANCE.convertList((t.getDetails())));
        });
    }

    @Override
    public List<UserPoolConfigDetailVo> getConfig(String siteUserCode) {
        List<UserPoolConfigDetailDo> list = userPoolConfigDetailMapper.selectListBySiteUserCode( siteUserCode);
        return UserPoolConfigDetailConvert.INSTANCE.convertList2(list);
    }
    @Override
    public List<UserPoolConfigDetailVo> getAllConfig() {
        List<UserPoolConfigDetailDo> list = userPoolConfigDetailMapper.selectList( );
        return UserPoolConfigDetailConvert.INSTANCE.convertList2(list);
    }


    @Override
    public void updateConfig(UserPoolConfigVo vo) {
        userPoolConfigDetailMapper.updateBatch(UserPoolConfigDetailConvert.INSTANCE.convertList(vo.getDetails()));
    }

    /**
     * 删除二级配置
     *
     * @param code
     */
    @Override
    public void deleteBySiteCode(String code) {
        userPoolConfigDetailMapper.deleteBySiteCode(code);
    }
    /**
     * 更具一级编码删除二级配置
     *
     * @param siteUserCode
     */
    @Override
    public void deleteBySiteUserCode(String siteUserCode) {
        userPoolConfigDetailMapper.deleteBySiteUserCode(siteUserCode);
    }
}
